using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Dss.ServiceModel.Dssp;

namespace ProMRDS.Robotics.StingerDriveByWire
{
    public partial class StingerDriveByWireForm : Form
    {
        StingerDriveByWireOperations _mainPort;

        public StingerDriveByWireForm(StingerDriveByWireOperations mainPort)
        {
            _mainPort = mainPort;

            InitializeComponent();
        }

        private void StingerDriveByWireForm_Load(object sender, EventArgs e)
        {
            _mainPort.PostUnknownType(new Load());
        }

        #region Invoked Methods

        // Set the Wander checkbox to reflect the current state
        public void SetWander(bool wander)
        {
            chkWander.Checked = wander;
        }

        // Set the Wander Mode to show the current mode
        public void SetWanderMode(string mode)
        {
            lblWanderMode.Text = mode;
        }

        // Update an IR sensor value on the form
        public void SetIRReadings(int pin, double distance)
        {
            switch (pin)
            {
                case 0:
                    lblIRLeft.Text = distance.ToString("f1");
                    break;

                case 1:
                    lblIRFront.Text = distance.ToString("f1");
                    break;

                case 2:
                    lblIRRight.Text = distance.ToString("f1");
                    break;

                default:
                    break;
            }
        }

        #endregion

        #region Motion Commands

        // NOTE: The settings of the motor power in these routines can be
        // changed to affect the behavior of the robot. They should really
        // be in the config file and accessible via an options dialog, but
        // that is a whole lot more work ...

        private void Stop()
        {
            // Use PostUnknownType for CF compatibility
            _mainPort.PostUnknownType(new MotionCommand(0, 0));
        }

        private void Forward()
        {
            // Use PostUnknownType for CF compatibility
            // This sample sets the power to 75%.  
            // Depending on your robotic hardware,
            // you may wish to change these values.
            _mainPort.PostUnknownType(new MotionCommand(75, 75));
        }

        private void Backward()
        {
            // Use PostUnknownType for CF compatibility
            _mainPort.PostUnknownType(new MotionCommand(-60, -60));
        }

        private void TurnLeft()
        {
            // Use PostUnknownType for CF compatibility
            _mainPort.PostUnknownType(new MotionCommand(-50, 50));
        }

        private void TurnRight()
        {
            // Use PostUnknownType for CF compatibility
            _mainPort.PostUnknownType(new MotionCommand(50, -50));
        }

        #endregion

        #region Button Handlers

        // Handle the Wander checkbox
        // OK, so this is a checkbox, but it is sort of like a button ...
        private void chkWander_CheckStateChanged(object sender, EventArgs e)
        {
            // Build a request so we can pass back the setting of the Wander checkbox
            Wander wander = new Wander(chkWander.Checked);
            // Tell main service what to do
            _mainPort.PostUnknownType(wander);
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            // Try to stop the robot first ...
            Stop();

            // Tell main service to quit
            _mainPort.PostUnknownType(new Quit());
            //this.Close();
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            Stop();
        }

        private void btnForward_Click(object sender, EventArgs e)
        {
            Forward();
        }

        private void btnBackward_Click(object sender, EventArgs e)
        {
            Backward();
        }

        private void btnTurnLeft_Click(object sender, EventArgs e)
        {
            TurnLeft();
        }

        private void btnTurnRight_Click(object sender, EventArgs e)
        {
            TurnRight();
        }

        #endregion

        #region Key Handlers

        private void StingerDriveByWireForm_KeyDown(object sender, KeyEventArgs e)
        {
            switch ((Keys)e.KeyValue)
            {
                case Keys.Up:
                case Keys.W:
                    Forward();
                    e.Handled = true;
                    break;

                case Keys.Down:
                case Keys.S:
                    Backward();
                    e.Handled = true;
                    break;

                case Keys.Left:
                case Keys.A:
                    TurnLeft();
                    e.Handled = true;
                    break;

                case Keys.Right:
                case Keys.D:
                    TurnRight();
                    e.Handled = true;
                    break;

                case Keys.Escape:
                    btnExit_Click(sender, e);
                    e.Handled = true;
                    break;

                default:
                    break;
            }
        }

        // Stop the motors any time that a key is released
        private void StingerDriveByWireForm_KeyUp(object sender, KeyEventArgs e)
        {
            Stop();
        }

        #endregion

    }
}